package com.yonyou.sto.modules.ims.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.sto.common.api.CommonPage;
import com.yonyou.sto.common.api.CommonResult;
import com.yonyou.sto.common.domain.DownloadFile;
import com.yonyou.sto.modules.ims.dto.TaxItemDto;
import com.yonyou.sto.modules.ims.model.ImsTaxItem;
import com.yonyou.sto.modules.ims.service.ImsTaxItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.security.Principal;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author aegon
 * @since 2020-09-15
 */
@RestController
@RequestMapping("/server/ims/imsTaxItem")
@Api(tags = "ImsTaxItemController", description = "税目管理")
public class ImsTaxItemController {

    @Resource
    ImsTaxItemService imsTaxItemService;

    @ApiOperation(value = "新增税目")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult create(@RequestBody TaxItemDto dto) {
        boolean success = imsTaxItemService.create(dto);
        if (success) {
            return CommonResult.success("success");
        }
        return CommonResult.failed("税目编码重复");
    }

    @ApiOperation(value = "修改税目关联组织")
    @PostMapping(value = "/updateParty")
    public CommonResult<String> updateParty(@RequestBody TaxItemDto dto){
        return imsTaxItemService.updateParty(dto);
    }

    @ApiOperation(value = "税目详情")
    @GetMapping(value = "/info/{id}")
    public CommonResult<TaxItemDto> info(@PathVariable Long id) {
        return imsTaxItemService.info(id);
    }

    @ApiOperation(value = "修改税目")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    public CommonResult update(@PathVariable Long id, @RequestBody TaxItemDto dto) {
        return imsTaxItemService.updateTax(id, dto);
    }

    @ApiOperation(value = "删除税目")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult delete(@RequestParam Long id) {
        boolean success = imsTaxItemService.delete(id);
        if (success) {
            return CommonResult.success("success");
        }
        return CommonResult.failed("有此组织的收款单,不能删除");
    }

    @ApiOperation(value = "根据税目编码名称分页查询税目")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<ImsTaxItem>> list(Principal principal,
                                                     @RequestParam(value = "keyword", required = false) String keyword,
                                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<ImsTaxItem> imsTaxItemPage = imsTaxItemService.list(principal, keyword, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(imsTaxItemPage));
    }

    @ApiOperation(value = "下载导入税目模板")
    @GetMapping(value = "/downloadFile")
    public void downloadFile(HttpServletRequest request, HttpServletResponse response){
        try {
            InputStream is = this.getClass().getClassLoader().getResourceAsStream("taxItemTemplate.xlsx");
            byte[] fileData = DownloadFile.input2byte(is);
            DownloadFile.downloadFile(response, request, "taxItemTemplate.xlsx", fileData);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    @ApiOperation(value = "导入税目信息")
    @PostMapping(value = "/importTax")
    public CommonResult importTax(MultipartFile file) {
        return imsTaxItemService.importTax(file);
    }
}

